package com.linln.admin.system.web;

import com.github.pagehelper.Page;
import com.linln.common.utils.HttpServletUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.system.dto.ExpressDto;
import com.linln.modules.system.entity.ProdSub;
import com.linln.modules.system.entity.WxUser;
import com.linln.modules.system.service.ProductServices;
import com.linln.modules.system.service.UserOrderServices;
import com.linln.modules.system.service.WxUserServices;
import com.linln.modules.system.vo.UserOrderDetailVO;
import com.linln.modules.system.vo.admins.OrderProductVo;
import com.linln.modules.system.vo.admins.ProductDetailVo;
import com.linln.modules.system.vo.admins.UserOrderAddress;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/mains/orders")
public class UserOrderController extends BaseController{

    @Autowired
    private UserOrderServices userOrderServices;

    @Autowired
    private ProductServices productServices;

    @RequestMapping(value = "/index")
    @RequiresPermissions("mains:orders:index")
    public ModelAndView getIndex(String username,String mobile,Integer pageNum,Integer pageSize) {
        pageNum = pageNum == null ? super.pageNum : pageNum;
        pageSize = pageSize == null ? super.pageSize : pageSize;
        ModelAndView view = new ModelAndView();
        view.setViewName("/mains/orders/index");
        Page<UserOrderDetailVO> company = userOrderServices.getIndexUserOrder(pageNum, pageSize, username,mobile);
        view.addObject("list", company.getResult());
        view.addObject("page", company);
        return view;
    }


    /**
     * 详细信息
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "express/{id}")
    public String expressOrder(@PathVariable("id") String id,Model model){
        try {
            UserOrderAddress address = userOrderServices.userOrderAddressVoByOrderId(id, id);
            model.addAttribute("address",address);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/mains/orders/express";
    }
     // OrderProductVo






    @ResponseBody
    @PostMapping(value = "expressUp")
    public ResultVo toDelete(String orderId,String expressCode,String expressCompany){
        ResultVo vo = ResultVoUtil.error("操作失败");
        try {
            int i = userOrderServices.updateExpressOrder(orderId, expressCode,expressCompany);
            if(i>0){
                vo.setMsg("操作成功");
                vo.setCode(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }


    /**
     * 详细信息
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "detail/{id}")
    public String orderDetail(@PathVariable("id") String id,Model model){
        try {
            OrderProductVo address = userOrderServices.getOrderDetailById(id, id);

            if(address.getProdKinds()==2){
                ProdSub ps = productServices.getProdSub(address.getSubId());
                if(ps!=null){
                    address.setProductName(address.getProductName()+":"+ps.getSubname());
                }

                model.addAttribute("prodSub",ps);
            }
            model.addAttribute("productOrder",address);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/mains/orders/detail";
    }
    @ResponseBody
    @RequestMapping(value = "/verifyOrder/{orderId}")
    public ResultVo verifyOrderStates(@PathVariable("orderId") String orderId) {
        userOrderServices.verifyOrder(orderId);
        ResultVo success = ResultVoUtil.success("发货填写成功!");
        success.setData("submit[refresh]");
        return success;
    }
    @RequestMapping(value = "/administration/{orderId}")
    @ResponseBody
    public ModelAndView administration(@PathVariable("orderId") String orderId) {
        ModelAndView view = new ModelAndView();
        view.setViewName("/mains/orders/adminis");
        Integer tradeState =  userOrderServices.getOrderStates(orderId);
        view.addObject("tradeState",tradeState);
        view.addObject("orderId",orderId);
        return view;
    }

    @RequestMapping(value = "/updateOrder")
    @ResponseBody
    public String updateOrder(String orderId,String orValue) {
        if("0".equals(orValue)){
            Date data =new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time =  sdf.format(data);
            int state =  userOrderServices.updateState(orderId,time);
            if(state>0){
                return "操作成功！";
            }else{
                return "操作失败！";
            }

        }else{
            int count = userOrderServices.delOrderById(orderId);
            if(count >0){
                return "操作成功！";
            }else{
                return "删除失败！";
            }
        }

    }

}
